function rt_driver_Sandbox(t_interval, switches, siteIDs)
    % Script to drive rt suite
    %
    % (Typical) Usage:
    %
    % >> rt_driver()
    %
    % results in the following default values being used:
    %
    % t_interval = [(now - 14 days) now], where times are "serial date numbers" 
    %              (see MATLAB help for more information on sdn's)
    %              To specify a different interval, provide it as a string* in 
    %              the format [<sdn(start)> <sdn(stop)>] where <sdn(start)>
    %              (resp. stop) is the beginning (resp. end) of the time interval 
    %              as a serial date number.
    %
    % switches   = containers.Map({'ascii', 'plot', 'ftp', 'close_plot', 'exit'}, [1 1 1 1 1]),
    %              where:
    %              the value associated with 'ascii' controls whether text output is generated, 
    %              'plot' controls whether graphic output is generated, 
    %              'ftp' controls the uploading of the results, and 
    %              'close_plots' controls whether the plots are closed (1) or left open (0) after
    %                  being rendered on the screen.
    %              'exit' determines whether MATLAB is exited at completion
    %              The default value of 1 for all of these produces all output, uploads it all,
    %              destroys the plots after being rendered and saved, and closes MATLAB at the end.
    %              To switch any of these to zero, include in a string argument its identifier, 
    %              a space, and then the digit 0, followed by a semi-colon to separate multiple
    %              pairs.  For example, to turn "off" everything but plot generation, include as 
    %              the second argument the string*:
    %              [' or "]ascii 0; ftp 0; close_plot 0; exit 0[', resp. "] 
    %              (See MATLAB help for more information on MATLAB Map container objects.)
    %              
    % siteIDs    = {'MCH01BR', 'MUK01BR', 'SQX01CF', 'SQX01SF', 'WPA13CR'}, where:
    %              'MCH01BR' => Manchester Station, 'MUK01BR' => Mukilteo Station, 
    %              'SQX01CF' & 'SQX01SF' => Squaxin Island Station, bottom and surface, resp.,
    %              'WPA13CR' => Wilapa Station.
    %              To specify a smaller subset of these, simply list them, space-separated, in a 
    %              single string*, e.g., [' or "]MUK01BR SQX01CF SQX01SF[', resp. "] will limit 
    %              processing to just the Mukilteo and both Squaxin stations.
    %
    % * To provide an argument as a string: from the MATLAB command prompt, use single quotes, 
    %   i.e., 'string', but from a Windows (DOS) command prompt, use double quotes, i.e., "string"
    
    import System.*
    import System.DateTime.*
    import containers.*
    MS2MATLAB_DTOFFSET = 693960; % Difference between MATLAB's and Microsoft's sdn's; 
                                 % necessary because the database server is a Microsoft product.
                                 
    % Set defaults, as necessary

    if nargin < 1 || isempty(t_interval)
        present = DateTime.UtcNow.ToOADate + MS2MATLAB_DTOFFSET;
        twoweeksago = present - 14;
        t_interval = [twoweeksago, present];
    else
        tmp = strtok(strtok(t_interval, ']'), '[');
        [start, stop] = strtok(tmp, ' ');
        t_interval = [str2double(start) str2double(stop)];
    end
    
    if nargin < 2 || isempty(switches)
        switches = Map({'ascii', 'plot', 'ftp', 'close_plot', 'exit'}, [1 1 1 1 1]);
    else
        tmp = Map({'ascii', 'plot', 'ftp', 'close_plot', 'exit'}, [1 1 1 1 1]);
        [start, stop] = strtok(switches, ';');
        while ~isempty(stop)
            [key, value] = strtok(start, ' ');
            tmp(key) = tmp(key) && str2double(value);
            [start, stop] = strtok(stop, ';');
        end
        switches = tmp;
    end

    if nargin < 3 || isempty(siteIDs)
        siteIDs = {'MCH01BR', 'MUK01BR', 'SQX01CF', 'SQX01SF', 'WPA13CR'};
    else
        [start, stop] = strtok(siteIDs, ' ');
        siteIDs = {start};
        while ~isempty(stop)
            [start, stop] = strtok(strtrim(stop), ' ');
            siteIDs = union(siteIDs, strtrim(start));
        end        
    end
    
    ftp_closed = true;
    % For each site
    for i=1:length(siteIDs)
        siteID = siteIDs{i};
        % Get to or create the right directory and confirm
        switch siteID(1:3)
            case 'MCH'
                localpath = 'Output\Manchester';
                path = '/eap/Mooring_Raw/Puget_Sound/Manchester/';
            case 'MUK'
                localpath = 'Output\Mukilteo';
                path = '/eap/Mooring_Raw/Puget_Sound/Mukilteo/';
            case 'SQX'
                localpath = 'Output\Squaxin';
                path = '/eap/Mooring_Raw/Puget_Sound/Squaxin/';
            case 'WPA'
                localpath = 'Output\Willapa';
                path = '/eap/Mooring_Raw/Willapa_Bay/Bay_Center_WPA13/';
            otherwise
                h = errordlg('Error in rt_driver.m, first switch block: ',...
                             'siteID not recognized!');
        end    
        cdworked = 1;
        if isdir(strcat('.\', localpath))
            cd(strcat('.\', localpath));
        elseif isdir(strcat('..\..\', localpath))
            cd(strcat('..\..\', localpath));
        else
            mdsuccessful = mkdir(strcat('\', localpath));
            if mdsuccessful
                cd(strcat('\', localpath));
            else
                cdworked = 0;
            end
        end
        fnameroot = strcat(siteID, '_REALTIME');
        
        % Get the data
        jointdata = rt_queries_Sandbox('joint', {siteID, t_interval});
        sitedata = rt_queries_Sandbox('site', {siteID});
        
        % convert and assemble the data we have
        [~, ~, bi] = intersect({siteID}, sitedata(:,1));
        if length(jointdata(1,:)) < 3
            t = t_interval(:); P = {}; T = {}; C = {}; S = {}; D = {}; DOS = {};
        else
            t = jointdata(:,3);
            P = jointdata(:,4);
            T = jointdata(:,5);
            C = jointdata(:,6);
            S = jointdata(:,7);
            V = jointdata(:,8);
            D = jointdata(:,9);
            DOS = jointdata(:,10);
        end
        nt = length(t); tnum = datenum(t(:));
        
        equipdata = rt_queries_Sandbox('equipment', {siteID, t_interval});
        [nr, ~] = size(equipdata);
        % Grab the UTCDeployDatetimes
        UTCdpy = datenum(equipdata(:,4)); % Ordered by UTCDeployDatetime, ascendingly, i.e., earliest -> most recent
        % Figure out where the posted datetimes are relative to these
        place = ones(nt,1);
        for j=2:length(UTCdpy)
            place = place + (tnum > UTCdpy(j)); % This makes place larger for later times
        end
        changes = diff(place); % 0 where there's no change in deployment time from one posted time to the next, 
                               % 1 where there is, i.e., at the index of the last posted time before a change in deploy time;
                               % Note: length(changes) = length(t)-1 
        ichanges = [0 find(changes') nt]; % The indices where changes = 1, plus a couple extra to eliminate if's 
                                         % from the splitting loop below
        % How many distinct place values are there?
        stnct = length(ichanges)-1;
        % Now "deal" out the required data & QAQC it
        [equipID{1:stnct}, statID{1:stnct}, equipNom1{1:stnct}, UTCdeploy{1:stnct}, auxChan{1:stnct},...
             calibType{1:stnct}, resCol{1:stnct}, sID{1:stnct}] = deal(equipdata{nr-stnct+1:nr, :});
        for j=1:stnct
            assert(strcmp(sID(j), siteID), 'Error from SiteEquipment table: siteID mismatch!');
            tmp = datenum(UTCdeploy(j), 'yyyy-mm-dd HH:MM:SS');
            assert(tmp <= t_interval(2));
            if j > 1
                assert(tmp >= t_interval(1));
            end
        end
        % Now split the variables required for the DO calc according to where they belong relative to the deployment times
        DOreq = cell(5, stnct);
        for j=1:stnct
                DOreq{1,j} = P(ichanges(j)+1:ichanges(j+1));
                DOreq{2,j} = T(ichanges(j)+1:ichanges(j+1));
                DOreq{3,j} = V(ichanges(j)+1:ichanges(j+1));
                DOreq{4,j} = DOS(ichanges(j)+1:ichanges(j+1));
        end
        DOreq(5,:) = deal(equipID(:));
                
        invendata = rt_queries_Sandbox('inventory', {equipID});
        [nr, ~] = size(invendata);
        [equipNom2{1:nr}, serialNoDesc{1:nr}, equipTypeID{1:nr}, manufacturer{1:nr}, modelDesc{1:nr}] = ...
                deal(invendata{1:nr,:});
        for j=1:nr
            assert(ismember(equipNom1{i}, equipNom2), 'Error from EquipmentInventory table: EquipmentName mismatch!');
        end
        
        calibdata = rt_queries_Sandbox('calib', {equipID, calibType});
        [nr, ~] = size(calibdata);
        if nr > 1
            % Grab just the returned equipIDs and calibBegins
            [tequipID2{1:nr}, tcalibBegin{1:nr}] = deal(calibdata{1:nr, 1:2});
            j = 1; indices = []; % storage for the positions we want to keep
            for k=1:nr-1 % for each equipID, calibBegin pair
                if tequipID2{k} ~= tequipID2{k+1} % if the current and next equipIDs are different
                    % definitely keep the current position; moreover, if we're at the end of the loop,
                    % keep the next position as well.
                    if k == nr-1
                        indices = [indices j j+1];
                    else
                        indices = [indices j];
                        j = j + 1;
                    end
                else % if they're the same
                    % keep the current position if it preceeds a UTCDeployDatetime for that equipID
                    compare = datenum(UTCdeploy(tequipID2{k} == [equipID{:}]));
                    if datenum(tcalibBegin{k}) <= compare
                        indices = [indices j];
                        j = j + 1;
                    end
                    % and if we're at the end of the loop perform the same test for the next equipID
                    if k == nr-1
                        compare = datenum(UTCdeploy(tequipID2{nr} == [equipID{:}]));
                        if datenum(tcalibBegin{nr}) <= compare
                            indices = [indices j];
                        end
                    end    
                end
            end
        else
            indices = 1;
        end
        nr = length(indices);
        % Now grab only those rows that are "keepers"
        [equipID2{1:nr}, calibBegin{1:nr}, calibEnd{1:nr}, equipNom3{1:nr}, calibType2{1:nr}, calibForm{1:nr}, calibID{1:nr}] = ...
            deal(calibdata{indices, 1:7});
%        c(1:nr,1) = equipID2;
        c(1:nr,1) = equipID2;
        c(1:nr,2:14) = deal(calibdata(indices,8:20));
        for j=1:nr
            assert(ismember(equipID{i}, [equipID2{:}]), 'Error from Calibration table: EquipmentID mismatch!');
            assert(ismember(equipNom1{i}, equipNom3), 'Error from Calibration table: EquipmentName mismatch!');
            assert(ismember(calibType{i}, calibType2), 'Error from Calibration table: CalibrationTypeID mismatch!');
        end
        
        caltypdat = rt_queries_Sandbox('caltyp', {calibType});
        [nr, ~] = size(caltypdat);
        [calibType3{1:nr}, calibDesc{1:nr}] = deal(caltypdat{1:nr,:});
        for j=1:nr
            assert(ismember(calibType{i}, calibType3), 'Error from CalibrationType table: CalibrationTypeID mismatch!');
        end
        
        calformdat = rt_queries_Sandbox('calform', {calibForm});
        [nr, ~] = size(calformdat);
        [calibForm2{1:nr}, formNom{1:nr}, formDesc{1:nr}, storedProc{1:nr}, unitsDesc{1:nr}] = deal(calformdat{1:nr,:});
        for j=1:nr
            assert(ismember(calibForm2{i}, calibForm), 'Error from CalibrationFormula table: CalibrationFormulaID mismatch!');
        end
        
        % convert and assemble the rest of the data
        if length(jointdata(1,:)) < 3
            DO = {};
        else
            [~, ~, ~, ~, DO, PF] = calcDO('SBE', DOreq, c(:,1:7));
        end

        % Produce the ascii output
        if switches('ascii')
            rt_writedat(fnameroot, sitedata, bi, t, P, T, C, S, D, DOS, DO, calibID, ichanges)%, siteID, t_interval);
        end
        
        % Produce the plots
        if switches('plot')
            if ftp_closed
                server = 'www.ecy.wa.gov';
                try
                    ftpobj = ftp(server, 'eapuser', 'Un10n$$');
                catch FE
                    h = errordlg('Error trying to get the plot captions',...
                                 'Error opening the ftp server');
                end
                ftp_closed = false;
            end
            cd(ftpobj, path);
            capfile = mget(ftpobj, strcat(siteID, '_WEBCAPTION.txt'));
            f = fopen(capfile{1});
            caption = fgetl(f);
            fclose(f);
            reqtext = Map({'Title', 'Caption'}, {sitedata(bi,1:4), caption});
            fh = rt_plotdat(t_interval, datenum(t), [P{:}], [T{:}], [S{:}], DO, reqtext);
            fname = strcat(fnameroot, '.png');
            print(fh, '-dpng', '-r150', fname);
            if switches('close_plot')
                close;
            end
        end
        
        % Post the results
        if switches('ftp')
            % Open ftp connection, upload file, and close connection
            if ftp_closed
                server = 'www.ecy.wa.gov';
                try
                    ftpobj = ftp(server, 'eapuser', 'Un10n$$');
                catch FE
                    h = errordlg('Error trying to post the results',...
                                 'Error opening the ftp server');
                end
                ftp_closed = false;
            end
            cd(ftpobj, path);
            mput(ftpobj, strcat(fnameroot, '*'));
%            close(ftpobj);
        end
        
        % If the earlier directory walking worked, change back to the "home" directory
        if cdworked
            cd('..\..');
        end
    end
    if ~ftp_closed
        close(ftpobj)
    end
    if switches('exit')
        exit force
    end